home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / comms / textvu10.zip / TEXTVU.PAS < prev   
Pascal/Delphi Source File  |  1993-08-23  |  5KB  |  207 lines

  1. (***************************************************************************
  2.   A Much Faster Terminal for Turbo Vision, v1.0
  3.   PJB August 22, 1993
  4.   Twice as fast, less code
  5.   Free source - no warranty
  6.   Send comments by Internet mail to d91-pbr@nada.kth.se
  7. ***************************************************************************)
  8. unit TextVu;
  9. {$O+,F+,X+,I-,S-}
  10.  
  11. interface
  12.  
  13.   uses Dos, Drivers, Objects, TextView, Views;
  14.  
  15.   type
  16.     PFastTerminal = ^TFastTerminal;
  17.     TFastTerminal = object (TTerminal)
  18.       constructor Init(var Bounds:TRect; AHScrollBar, AVScrollBar: PScrollBar;
  19.         ABufSize: Word);
  20.       procedure AddLine(var Buf; Count:integer);
  21.       function  CalcWidth: Integer;
  22.       function  CanInsert(Amount: Word): Boolean;
  23.       procedure Draw; virtual;
  24.       function  NextLines(Pos, Count:word):word;
  25.       procedure StrWrite(var S: TextBuf; Count: Byte); virtual;
  26.     end;
  27.  
  28. (***************************************************************************
  29. ***************************************************************************)
  30. implementation
  31.  
  32.   constructor TFastTerminal.Init(var Bounds:TRect; AHScrollBar,
  33.     AVScrollBar: PScrollBar; ABufSize: Word);
  34.   begin
  35.     TTerminal.Init(Bounds, AHScrollBar, AVScrollBar, ABufSize);
  36.     Limit.Y:=0;
  37.     HideCursor;
  38.   end;
  39.  
  40.   procedure TFastTerminal.AddLine(var Buf; Count:integer);
  41.     var
  42.       i : integer;
  43.       ReCalcWidth : boolean;
  44.   begin
  45.     ReCalcWidth:=False;
  46.     while not CanInsert(Count+1) do
  47.     begin
  48.       QueBack:=NextLines(QueBack,1);
  49.       dec(Limit.Y);
  50.       ReCalcWidth:=True;
  51.     end;
  52.  
  53.     if ReCalcWidth then
  54.       Limit.X:=CalcWidth;
  55.  
  56.     Buffer^[QueFront]:=Chr(Count);
  57.     inc(QueFront);
  58.     if QueFront>=BufSize then
  59.       QueFront:=0;
  60.  
  61.     if BufSize-QueFront<Count then
  62.     begin
  63.       i:=BufSize-QueFront;
  64.       Move(Buf,Buffer^[QueFront], i);
  65.       Move(TByteArray(Buf)[i], Buffer^, Count-i);
  66.       QueFront:=Count-i;
  67.     end
  68.     else
  69.     begin
  70.       Move(Buf, Buffer^[QueFront], Count);
  71.       inc(QueFront, Count);
  72.     end;
  73.  
  74.     if Count>Limit.X then
  75.       Limit.X:=Count;
  76.     inc(Limit.Y);
  77.   end;
  78.  
  79.   function TFastTerminal.CalcWidth:integer;
  80.   var
  81.     i, Width : integer;
  82.     CurPos   : integer;
  83.   begin
  84.     Width:=0;
  85.     CurPos:=QueBack;
  86.     for i:=1 to Limit.Y do
  87.     begin
  88.       if ord(Buffer^[CurPos])>Width then
  89.         Width:=ord(Buffer^[CurPos]);
  90.       CurPos:=NextLines(CurPos,1);
  91.     end;
  92.     CalcWidth:=Width;
  93.   end;
  94.  
  95.   function TFastTerminal.CanInsert(Amount:word):boolean;
  96.   var
  97.     BufFree : word;
  98.   begin
  99.     if QueFront<QueBack then
  100.       BufFree:=QueBack-QueFront
  101.     else
  102.       BufFree:=BufSize-QueFront+QueBack;
  103.     CanInsert:=BufFree>Amount;
  104.   end;
  105.  
  106.   procedure TFastTerminal.Draw;
  107.   var
  108.     Buf       : TDrawBuffer;
  109.     Color     : byte;
  110.     CurPos    : integer;
  111.     i, LastY  : integer;
  112.     Len, Len1 : integer;
  113.   begin
  114.     Color:=GetColor(1);
  115.  
  116.     if Size.Y>Limit.Y then
  117.     begin
  118.       MoveChar(Buf, ' ', Color, Size.X);
  119.       WriteLine(0, Limit.Y, Size.X, Size.Y-Limit.Y, Buf);
  120.       LastY:=Limit.Y;
  121.     end
  122.     else
  123.       LastY:=Size.Y;
  124.  
  125.     CurPos:=NextLines(QueBack, Delta.Y);
  126.     for i:=0 to LastY-1 do
  127.     begin
  128.       Len:=ord(Buffer^[CurPos])-Delta.X;
  129.       if Len>Size.X then
  130.         Len:=Size.X;
  131.       if Len>0 then
  132.         if BufSize-Len-Delta.X>CurPos+1 then
  133.           MoveBuf(Buf, Buffer^[CurPos+1+Delta.X], Color, Len)
  134.         else
  135.         begin
  136.           Len1:=BufSize-CurPos-1-Delta.X;
  137.           MoveBuf(Buf, Buffer^[CurPos+1+Delta.X], Color, Len1);
  138.           MoveBuf(Buf[Len1], Buffer^, Color, Len-Len1);
  139.         end;
  140.       if Len<0 then
  141.         Len:=0;
  142.       MoveChar(Buf[Len], ' ', Color, Size.X-Len);
  143.       WriteLine(0, i, Size.X, 1, Buf);
  144.       CurPos:=NextLines(CurPos,1);
  145.     end;
  146.   end;
  147.  
  148.   function TFastTerminal.NextLines(Pos, Count:word):word; assembler;
  149.   asm
  150.       mov  bx,Pos
  151.       cmp  Count,0
  152.       je   @Fin
  153.  
  154.       les  di,Self
  155.       mov  dx,es:[di].BufSize
  156.  
  157.       les  di,es:[di].Buffer
  158.       mov  ah,0
  159.  
  160.     @Loop:
  161.       mov  al,es:[di+bx]
  162.       stc
  163.       adc  bx,ax
  164.       jc   @Fix
  165.       cmp  bx,dx
  166.       jb   @OK
  167.  
  168.     @Fix:
  169.       sub  bx,dx
  170.  
  171.     @OK:
  172.       dec  Count
  173.       jne  @Loop
  174.  
  175.     @Fin:
  176.       mov  ax,bx
  177.   end;
  178.  
  179.   procedure TFastTerminal.StrWrite(var S: TextBuf; Count: Byte);
  180.   var
  181.     i, j: Word;
  182.   begin
  183.     if Count>0 then
  184.     begin
  185.       j:=0;
  186.       for i:=0 to Count-1 do
  187.         if S[i]=#13 then
  188.         begin
  189.           AddLine(S[j], i-j);
  190.           j:=i+2;
  191.         end;
  192.  
  193.       Owner^.Lock;
  194.       SetLimit(Limit.X, Limit.Y);
  195.       ScrollTo(0, Limit.Y);
  196.       if Limit.Y<=Size.Y then
  197.         DrawView;
  198.       Owner^.Unlock;
  199.     end;
  200.   end;
  201.  
  202.  
  203.     (*******************************************************************
  204.     *******************************************************************)
  205.  
  206. end.
  207.